<!--  -->
<template>
  <el-form :label-width="formLabelWidth" :model="commonSet" @submit.native.prevent :rules="rules" class="compoform" ref="commonsetForm">
    <el-form-item label="公共组件" style="margin-bottom:0px;">
      <el-radio-group v-model="commonSet.isOpen">
        <el-radio :label="1">开启</el-radio>
        <el-radio :label="0">关闭</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="公共组件名称" style="margin-bottom:10px;" v-if="commonSet.isOpen" prop="name">
      <el-input size="mini" style="width:400px;"  v-model="commonSet.name" maxlength="100"
        placeholder="当公共组件开启时，此名称必填"></el-input>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  mounted () {
    if (!this.defValue) {
      this.commonSet.isOpen = 0
      this.commonSet.name = ''
    } else {
      this.commonSet.isOpen = 1
      this.commonSet.name = this.defValue
    }
  },
  props: {
    formLabelWidth: {
      type: String,
      default: '120px'
    },
    defValue: {
      type: String
    },
    chgValue: {
      type: Function
    }
  },
  data () {
    return {
      commonSet: {
        isOpen: 0,
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '当公共组件开启时，公共组件名称必填', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    commonSet: {
      handler (newValue) {
        if (newValue.isOpen === 1 && newValue.name !== '') {
          this.chgValue(newValue.name)
        } else {
          this.chgValue(null)
        }
      },
      deep: true
    }
  },
  methods: {
    dataValid () {
      if (this.commonSet.isOpen === 1) {
        let bool = true
        this.$refs['commonsetForm'].validate((valid) => {
          if (valid) {
            bool = true
          } else {
            bool = false
          }
        })
        return bool
      } else {
        return true
      }
    }
  }
}

</script>
<style scoped>
.compoform /deep/ .el-form-item__error{
  padding-top:0;
}
</style>
